package com.example.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	Button btn_Play, btn_Options, btn_Instruction, btn_Quit;
	int Diff_option;
	int REQUEST_CODE_OPTION=10;
	// Hàm khởi tạo cho các biến Button
	private void Initial_Function()
	{
		btn_Play=(Button)findViewById(R.id.btn_play);
		btn_Options=(Button)findViewById(R.id.btn_options);
		btn_Instruction=(Button)findViewById(R.id.btn_instruction);
		btn_Quit=(Button)findViewById(R.id.btn_quit);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Initial_Function();
		Diff_option=0;
		btn_Play.setOnClickListener(this);
		btn_Options.setOnClickListener(this);
		btn_Instruction.setOnClickListener(this);
		btn_Quit.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_play:
			Intent PlayActivity=new Intent(MainActivity.this, Play_Activity.class);
			PlayActivity.putExtra("Diff_Option", Diff_option);
			startActivityForResult(PlayActivity, 0);
			break;
		case R.id.btn_options:
			Intent OptionsActivity=new Intent(MainActivity.this, Options_Activity.class);
			OptionsActivity.putExtra("Diff_Option", Diff_option);
			startActivityForResult(OptionsActivity, REQUEST_CODE_OPTION);
			break;
		case R.id.btn_instruction:
			Intent InstructionActivity=new Intent(MainActivity.this, Instruction_Activity.class);
			startActivityForResult(InstructionActivity, 0);
			break;
		case R.id.btn_quit:
			AlertDialog dialogBox = makeAndShowDialogBox();
			dialogBox.show();
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    // Check which request we're responding to
	    if (requestCode == REQUEST_CODE_OPTION) {
	        if (resultCode == RESULT_OK) {
	        	Bundle Return_diff_option = data.getExtras();
	        	Diff_option=Return_diff_option.getInt("Return_Diff_Option");
	        }
	    }
	}
	
	// Tạo AlertDialog
	private AlertDialog makeAndShowDialogBox(){
		AlertDialog myQuittingDialogBox =
		new AlertDialog.Builder(this)
		// thiết lập tiêu đề và thông điệp
		.setTitle("Termination")
		.setMessage("Are you sure that you want to quit the game?")		
		
		//xử lý khi người chơi chọn YES
		.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				finish();
				return;
			}
		})
		// xử lý khi người chơi chọn NO
		.setNegativeButton("NO", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				return;
			}
		})
		
		.create();
		return myQuittingDialogBox;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
